package com.baihan.angus.app.game.common.util.media;

import android.media.MediaPlayer;
import android.view.SurfaceHolder;

import java.io.IOException;

public abstract class MyVideoPlayer implements MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
    private static final String TAG = "MyVideoPlayer";
    private String path;
    private boolean isInitFinish = false;
    private MediaPlayer mediaPlayer;
    private boolean isStop = true;

    public MyVideoPlayer(String path) {
        this.path = path;
        initMediaPlayer();
    }

    private void initMediaPlayer() {
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(path);//
            mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);//缩放模式
//            mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
            mediaPlayer.setLooping(false);//设置循环播放
            mediaPlayer.prepareAsync();//异步准备
            //当装载流媒体完毕的时候回调。
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnVideoSizeChangedListener(this);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnErrorListener(this);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 给mMediaPlayer添加预览的SurfaceHolder
     *
     * @param holder
     */
    public void setDisplay(SurfaceHolder holder) {
        mediaPlayer.setDisplay(holder);
    }

    public boolean playVideo() {
        if (mediaPlayer != null && isInitFinish) {
            mediaPlayer.start();
            return true;
        }
        return false;
    }

    public boolean isInitFinish() {
        return isInitFinish;
    }

    public boolean isPlaying() {
        if (mediaPlayer == null) return false;
        return mediaPlayer.isPlaying();
    }

    /**
     * 暂停播放
     */
    public boolean pausePlay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            return true;
        }
        return false;
    }

    /**
     * 重新播放
     */
    public boolean replayPlay() {
        if (mediaPlayer != null) {
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
            isStop = false;
            return true;
        }
        return false;
    }

    public boolean isStop() {
        return isStop;
    }

    /**
     * 停止播放
     */
    public boolean stopPlay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            isStop = true;
            mediaPlayer.stop();
            isInitFinish = false;
            mediaPlayer.prepareAsync();
            return true;
            //mediaPlayer.release();
            //mediaPlayer = null;
        }
        return false;
    }

    /**
     * 循环播放
     *
     * @param looping
     */
    public void setLooping(boolean looping) {
        mediaPlayer.setLooping(looping);
    }

    /**
     * 获取总时长
     *
     * @return
     */
    public int getDuration() {
        int duration = mediaPlayer.getDuration();
        return duration;
    }

    /**
     * 获取当前播放的位置
     *
     * @return
     */
    public int getCurrentPosition() {
        int currentPosition = mediaPlayer.getCurrentPosition();
        return currentPosition;
    }

    /**
     * 设置当前MediaPlayer的播放位置，单位是毫秒
     *
     * @param progress
     */
    public void setSeekto(int progress) {
        mediaPlayer.seekTo(progress);//在当前位置播放
    }

    /**
     * 释放资源
     */
    public void release() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            //回收流媒体资源
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    /**
     * 传入的数据为毫秒数
     *
     * @param time
     * @return
     */
    public String formattime(long time) {
        String min = (time / (1000 * 60)) + "";
        String second = (time % (1000 * 60) / 1000) + "";
        if (min.length() < 2) {
            min = 0 + min;
        }
        if (second.length() < 2) {
            second = 0 + second;
        }
        return min + ":" + second;
    }

    /**
     * 计算播放时间
     *
     * @param time
     * @return
     */
    public String calculateTime(int time) {
        int minute;
        int second;
        if (time > 60) {
            minute = time / 60;
            second = time % 60;
            //分钟再0~9
            if (minute >= 0 && minute < 10) {
                //判断秒
                if (second >= 0 && second < 10) {
                    return "0" + minute + ":" + "0" + second;
                } else {
                    return "0" + minute + ":" + second;
                }
            } else {
                //分钟大于10再判断秒
                if (second >= 0 && second < 10) {
                    return minute + ":" + "0" + second;
                } else {
                    return minute + ":" + second;
                }
            }
        } else if (time < 60) {
            second = time;
            if (second >= 0 && second < 10) {
                return "00:" + "0" + second;
            } else {
                return "00:" + second;
            }
        }
        return null;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        isInitFinish = true;
    }

}
